package com.ktkj.init.controller;


import com.ktkj.init.dto.RoleInfoCondition;
import com.ktkj.init.dto.RoleInfoForm;
import com.ktkj.init.entity.RoleInfo;
import com.ktkj.init.exception.BusinessException;
import com.ktkj.init.response.Response;
import com.ktkj.init.response.ResponseCallBack;
import com.ktkj.init.response.ResponseCriteria;
import com.ktkj.init.service.RoleInfoService;
import com.ktkj.init.vo.QueryResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 角色信息表 前端控制器
 * </p>
 *
 * @author ktkj
 * @since 2021-12-31
 */
@Api(tags = "角色信息管理-后台")
@RestController
@RequestMapping("/roleInfo")
public class RoleInfoController {

    @Resource
    private RoleInfoService roleInfoService;

    @ApiOperation(value = "详情")
    @GetMapping(path = "/getById")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "id", example = "1", required = true)})
    public Response getById(@RequestParam final Long id) {
        return new ResponseCallBack() {
            @Override
            public void execute(ResponseCriteria criteria, Object... obj) {
                RoleInfo bean = roleInfoService.getById(id);
                if (bean == null) {
                    throw new BusinessException(-1, "id错误，没有查询到对应数据！");
                }
                criteria.addSingleResult(bean);
            }
        }.sendRequest();
    }

    @ApiOperation(value = "列表")
    @GetMapping(path = "/list")
    public Response list(@Validated @ModelAttribute final RoleInfoCondition condition) {
        return new ResponseCallBack() {
            @Override
            public void execute(ResponseCriteria criteria, Object... obj) {
                QueryResult<RoleInfo> queryList = roleInfoService.page(condition);
                criteria.addSingleResult(queryList);
            }
        }.sendRequest();
    }

    @ApiOperation(value = "保存（增加/修改）")
    @PostMapping(path = "/saveOrUpdate")
    public Response saveOrUpdate(@Validated @ModelAttribute final RoleInfoForm form) {
        return new ResponseCallBack() {
            @Override
            public void execute(ResponseCriteria criteria, Object... obj) {
                RoleInfo bean = roleInfoService.saveOrUpdate(form);
                criteria.addSingleResult(bean);
            }
        }.sendRequest();
    }

    @ApiOperation(value = "批量删除")
    @PostMapping(path = "/batchDelete")
    @ApiImplicitParams({@ApiImplicitParam(name = "ids", value = "id集合（多个id用,隔开）", example = "1,2,3", required = true)})
    public Response batchDelete(@RequestParam final String ids) {
        return new ResponseCallBack() {
            @Override
            public void execute(ResponseCriteria criteria, Object... obj) {
                List<Long> idList = Arrays.asList(ids.trim().split(",")).stream().map(s -> Long.parseLong(s.trim())).collect(Collectors.toList());
                roleInfoService.removeByIds(idList);
                criteria.addSingleResult(true);
            }
        }.sendRequest();
    }
}
